package cn.edu.sjtu.se.dslab.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.sjtu.se.dslab.action.bean.PageBean;
import cn.edu.sjtu.se.dslab.auth.User;
import cn.edu.sjtu.se.dslab.component.method.MethodComponent;
import cn.edu.sjtu.se.dslab.dao.IMethodComponentDao;
import cn.edu.sjtu.se.dslab.dao.IUserDao;
import cn.edu.sjtu.se.dslab.exception.DBException;
import cn.edu.sjtu.se.dslab.service.IMethodComponentService;

@Service("methodComponentService")
@Transactional
public class MethodComponentServiceImpl implements IMethodComponentService {

	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	@Resource(name = "methodComponentDao")
	private IMethodComponentDao methodComponentDao;
	
	@Resource(name = "userDao")
	private IUserDao userDao;

	@Override
	public boolean addMethodComponent(MethodComponent component,long userId) {
		// TODO Auto-generated method stub
		User user = userDao.queryById(userId);
		component.setOwner(user);
		methodComponentDao.save(component);
		return true;
	}

	@Override
	public MethodComponent getMethodComponentById(long id) {
		// TODO Auto-generated method stub
		return methodComponentDao.queryById(id);
	}

	@Override
	public boolean updateMethodComponent(MethodComponent component) {
		// TODO Auto-generated method stub
		methodComponentDao.update(component);
		return true;
	}

	public IMethodComponentDao getMethodComponentDao() {
		return methodComponentDao;
	}

	public void setMethodComponentDao(IMethodComponentDao methodComponentDao) {
		this.methodComponentDao = methodComponentDao;
	}

	@Override
	public PageBean<MethodComponent> getPageBean(int pageNo) throws DBException {
		// TODO Auto-generated method stub
		return methodComponentDao.queryByPageNum(pageNo);
	}

	@Override
	public MethodComponent getMethodComponentByUri(String uri) {
		// TODO Auto-generated method stub
		return methodComponentDao.queryByUri(uri);
	}

	@Override
	public boolean addMethodComponent(MethodComponent component) {
		// TODO Auto-generated method stub
		methodComponentDao.save(component);
		return true;
	}

	@Override
	public List<MethodComponent> getMethodComponentByName(String name) {
		// TODO Auto-generated method stub
		return methodComponentDao.queryByName(name);
	}

	@Override
	public List<MethodComponent> getAllMethodComponents() {
		// TODO Auto-generated method stub
		return methodComponentDao.queryAll();
	}

	@Override
	public List<MethodComponent> getMethodComponentsByText(String text) {
		// TODO Auto-generated method stub
		return methodComponentDao.searchComponentByText(text);
	}

}
